//
//  HABleManager.h
//  EKLLighting
//
//  Created by Chris on 2021/4/30.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "HABleModel.h"
#import "CBPeripheral+RSSI.h"

#define _weakself __weak __typeof(&*self) weakself = self

#define KRServiceUUID             @"F1CEA54F-449A-8717-0042-534332310A01"
#define KRCharacteristicWrtieUUID @"F1CEA54F-449A-8717-0042-534332310B01"
#define KRCharacteristicReadUUID  @"F1CEA54F-449A-8717-0042-534332310B02"


#define BLUETOOTHVERIFYSUCCESS      @"BlueToothVerifySuccess"
#define BLUETOOTHDISCONNECTED       @"BlueToothDisConnected"
#define BLUETOOTHPOWERSTATUSCHANGE  @"BlueToothPowerStatusChange"

NS_ASSUME_NONNULL_BEGIN

//ble
typedef void (^SuccessConnectToPeriperalBlock)(CBCentralManager *central, CBPeripheral *peripheral);
typedef void (^FailConnectToPeriperalBlock)(CBCentralManager *central, CBPeripheral *peripheral, NSError *error);
typedef void (^CancelConnectToPeriperalBlock)(CBCentralManager *central, CBPeripheral *peripheral, NSError *error);
typedef void (^DiscoverPeriperalsBlock)(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary<NSString *, id> * advertisementData, NSNumber *RSSI);
typedef void (^DiscoverCharacteristicBlock)(CBPeripheral *CBPeripheral, CBService *service, NSError *error);

@protocol HABleManagerDelegate<NSObject>

@optional
- (void)centralHAManagerDidUpdateState:(CBCentralManager *)central;

@end



@interface HABleManager : NSObject<CBCentralManagerDelegate,CBPeripheralDelegate>

@property (nonatomic,assign) id<HABleManagerDelegate> delegate;

@property(nonatomic, strong) CBPeripheral *currentPeri;

@property(nonatomic, strong) CBCentralManager *centralManager;


+ (HABleManager *)sharedManager;

- (void)scanPeriperals:(_Nullable DiscoverPeriperalsBlock)discoverPeriperals;

- (void)stopScanPeriperals;

- (void)connectToPeripheral:(CBPeripheral *)peripheral success:(SuccessConnectToPeriperalBlock)success fail:(FailConnectToPeriperalBlock)fail characteristic:(DiscoverCharacteristicBlock)characteristic;

- (void)cancelConnectToPeripheral:(_Nullable CancelConnectToPeriperalBlock)cancel;

- (void)readRSSIWithPeriperal:(CBPeripheral *)peripheral;

+ (void)sendCommandData:(NSData *)command completion:(void (^ _Nullable)(HABleModel *bleModel))completion;


@end

NS_ASSUME_NONNULL_END
